﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.ServiceModel;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
namespace RssReader
{
    public partial class FeedDetailsPage : PhoneApplicationPage
    {
        object _selectedFeedItem;
        object _selectedFeed;
        public FeedDetailsPage()
        {
            #region attach handlers
            InitializeComponent();
            SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
            PageTransitionDetails.Completed += new EventHandler(PageTransitionDetails_Completed);
            Loaded += new RoutedEventHandler(PhoneApplicationPage_Loaded);
            PageTransitionList.Completed += new EventHandler(PageTransitionList_Completed);
            #endregion
            LoadFeed();
        }
        private void LoadFeed()
        {
            FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
            var currentFeed = root.DataContext as FeedViewModel;
            WebClient wc = new WebClient();
            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
            wc.DownloadStringAsync(new Uri(currentFeed.FeedUrl));
            _selectedFeed = currentFeed;
        }
        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            var rssFeed = XElement.Parse(e.Result);
            var currentFeed = this.DataContext as FeedViewModel;
            var items = from item in rssFeed.Descendants("item")
                        select new FeedItemViewModel()
                        {
                            Title = item.Element("title").Value,
                            DatePublished = DateTime.Parse(item.Element("pubDate").Value),
                            Url = item.Element("link").Value,
                            Description = item.Element("description").Value
                        };
            foreach (var item in items)
                currentFeed.Items.Add(item);

        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            ResetPageTransitionList.Begin();
            this.DataContext = _selectedFeed;
        }
        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // Cancel default navigation
            e.Cancel = true;
            // Do page ransition animation
            PageTransitionDetails.Begin();
        }
        void PageTransitionDetails_Completed(object sender, EventArgs e)
        {
            // Reset root frame to MainPage.xaml
            PhoneApplicationFrame root = (PhoneApplicationFrame)Application.Current.RootVisual;
            root.GoBack();
        }


        private void lstFeeds_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            // Capture selected item data
            _selectedFeedItem = (sender as ListBox).SelectedItem;

            // Start page transition animation
            PageTransitionList.Begin();
        }
        private void PageTransitionList_Completed(object sender, EventArgs e)
        {
            // Set datacontext of details page to selected listbox item
            NavigationService.Navigate(new Uri("/FeedItemDetailsPage.xaml", UriKind.Relative));
            FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
            root.DataContext = _selectedFeedItem;
        }
      
    }
}